package cn.com.enersun.iec61850.integratedDataProcessing;

import org.openmuc.openiec61850.*;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by YYC on 2017/4/24.
 */
public class EasyTreeJsonObject {
    private List<EasyTree> easyTreeJson;

    public EasyTreeJsonObject(ServerModel serverModel) throws Exception {
        if (serverModel == null) {
            throw new Exception("传入的ServerModel数据为空");
        }

        this.setEasyTreeJson(serverModel);
    }

    public EasyTreeJsonObject(String ip) throws Exception {
        if (ip == null) {
            throw new Exception("传入的ip数据为空");
        }

        IntegratedDataProcessing.clientDataProcessing.retrieveServerModelByNet(InetAddress.getByName(ip));
        this.setEasyTreeJson(IntegratedDataProcessing.clientServerModels.get(ip));
    }

    public EasyTreeJsonObject(String ip, String sclPath) throws Exception {
        if ((ip == null) || (sclPath == null)) {
            throw new Exception("传入的ip或sclPath数据为空");
        }

        IntegratedDataProcessing.clientDataProcessing.retrieveServerModelByScl(InetAddress.getByName(ip), sclPath);
        this.setEasyTreeJson(IntegratedDataProcessing.clientServerModels.get(ip));
    }

    public void setEasyTreeJson(ServerModel serverModel) {
        easyTreeJson = new ArrayList<EasyTree>();

        EasyTree goose = new EasyTree();
        goose.setText("GOOSE");
        goose.setUiIcon("icon-none");
        goose.setId("GOOSE");
        goose.setDisType("gList");
        goose.setChildren(null);
        easyTreeJson.add(goose);

        EasyTree dataSetTree = new EasyTree();
        dataSetTree.setText("DataSets");
        dataSetTree.setUiIcon("icon-none");
        dataSetTree.setId("DataSets");
        dataSetTree.setDisType("List");
        dataSetTree.setChildren(new LinkedList<EasyTree>());
        int i = 0;
        //得到逻辑设备
        for(ModelNode ld : serverModel.getChildren()){
            dataSetTree.getChildren().add(new EasyTree(
                    ld.toString(),
                    null,
                    "icon-ld",
                    "DataSets"+ld.getReference().toString().replaceAll("\\W",""),
                    "list",
                    new LinkedList<EasyTree>()));
            for(DataSet dataSet : serverModel.getDataSets()){
                if(dataSet.toString().contains(ld.toString())) {
                    dataSetTree.getChildren().get(i).getChildren().add(new EasyTree(
                            dataSet.toString().substring(ld.toString().length()+1, dataSet.toString().length()),
                            null,
                            "icon-ds",
                            "DataSets"+dataSet.getReferenceStr().replaceAll("\\W",""),
                            "table",
                            null));
                }
            }
            i++;
        }
        easyTreeJson.add(dataSetTree);

        EasyTree reportTree = new EasyTree();
        reportTree.setText("Reports");
        reportTree.setUiIcon("icon-none");
        reportTree.setId("Reports");
        reportTree.setDisType("list");
        reportTree.setChildren(new LinkedList<EasyTree>());
        //得到逻辑设备
        i = 0;
        int j = 0;
        for(ModelNode ld : serverModel.getChildren()){
            reportTree.getChildren().add(new EasyTree(
                    ld.getName(),
                    null,
                    "icon-ld",
                    "Reports"+ld.getReference().toString().replaceAll("\\W",""),
                    "list",
                    new LinkedList<EasyTree>()));
            //得到逻辑节点
            j=0;
            for(ModelNode ln : ld.getChildren()) {
                reportTree.getChildren().get(i).getChildren().add(new EasyTree(
                        ln.getName(),
                        null,
                        "icon-ln",
                        "Reports"+ln.getReference().toString().replaceAll("\\W",""),
                        "list",
                        new LinkedList<EasyTree>()));
                for(Brcb brcb : serverModel.getBrcbs()){
                    if(brcb.getParent().getReference().toString().equals(ln.getReference().toString())) {
                        reportTree.getChildren().get(i).getChildren().get(j).getChildren().add(new EasyTree(
                                brcb.getReference().toString().substring(ln.getReference().toString().length()+1,brcb.getReference().toString().length()),
                                null,
                                "icon-br",
                                "Reports"+brcb.getReference().toString().replaceAll("\\W",""),
                                "table",
                                null));
                    }
                }
                for(Urcb urcb : serverModel.getUrcbs()){
                    if(urcb.getParent().getReference().toString().equals(ln.getReference().toString())) {
                        reportTree.getChildren().get(i).getChildren().get(j).getChildren().add(new EasyTree(
                                urcb.getReference().toString().substring(ln.getReference().toString().length() + 1, urcb.getReference().toString().length()),
                                null,
                                "icon-ur",
                                "Reports"+urcb.getReference().toString().replaceAll("\\W",""),
                                "table",
                                null));
                    }
                }
                j++;
            }
            i++;
        }
        easyTreeJson.add(reportTree);

        EasyTree dataModel = new EasyTree();
        dataModel.setText("Data Model");
        dataModel.setUiIcon("icon-none");
        dataModel.setId("DataModel");
        dataModel.setDisType("list");
        dataModel.setChildren(new LinkedList<EasyTree>());
        //得到逻辑设备
        i = 0;
        for(ModelNode ld : serverModel.getChildren()){
            dataModel.getChildren().add(new EasyTree(
                    ld.getName(),
                    null,
                    "icon-ld",
                    "DataModel"+ld.getReference().toString().replaceAll("\\W",""),
                    "list",
                    new LinkedList<EasyTree>()));
            //得到逻辑节点
            for(ModelNode ln : ld.getChildren()) {
                dataModel.getChildren().get(i).getChildren().add(new EasyTree(
                        ln.getName(),
                        null,
                        "icon-ln",
                        "DataModel"+ln.getReference().toString().replaceAll("\\W",""),
                        "table",
                        null));
            }
            i++;
        }
        easyTreeJson.add(dataModel);

        EasyTree settingGroups = new EasyTree();
        settingGroups.setText("Setting Groups");
        settingGroups.setUiIcon("icon-none");
        settingGroups.setId("SettingGroups");
        settingGroups.setDisType("setList");
        settingGroups.setChildren(null);
        easyTreeJson.add(settingGroups);

        EasyTree files = new EasyTree();
        files.setText("Files");
        files.setUiIcon("icon-none");
        files.setId("Files");
        files.setDisType("fList");
        files.setChildren(null);
        easyTreeJson.add(files);
    }

    public List<EasyTree> getEasyTreeJson() {
        return easyTreeJson;
    }

    public void setEasyTreeJson(List<EasyTree> easyTreeJson) {
        this.easyTreeJson = easyTreeJson;
    }

    @Override
    public String toString() {
        return "EasyTreeJsonObject{" +
                "easyTreeJson=" + easyTreeJson +
                '}';
    }
}
